classdef SubjectInfo
    %UNTITLED
    
    properties
        Name;
        Description;
        FilepathBase; % Root folder of this subjects' store (used to derive subfolders)
        PrimaryPrefix; % Will be used to prefix files related to the subject
        OriginalData;
        Electrodes; % Hold information about all electrodes
        Datasets; % Array of DatasetInfo, provides the primary data store information
        Fieldtrip; % Structure that holds the filenames of fieldtrip layout and label files
        Analyses; % Structure that holds references to the performed analyses
        Tasks; % Structure that contains boolean values indicating the status of tasks
        EpochData; % Structure that contains information about epoched data
        RoiSelections; % Structure that contains manually selected regions of interest
    end
    
    methods
        
        function s = SubjectInfo
            s.OriginalData = OriginalDataInfo;
            s.Datasets = DatasetInfo;
            s.Electrodes = ElectrodeInfo;
            s.Tasks = TaskInfo;
        end
        
        function obj = set.Name(obj,name)
            if ~(ischar(name))
                error('Name must be a string')
            end
            obj.Name = name;
        end % Name set function
        
        function obj = set.Description(obj,description)
            if ~(ischar(description))
                error('Description must be a string')
            end
            obj.Description = description;
        end % Description set function
        
        function filename = Save(obj)
            %SAVESUBJECTINFO Save the subject information.
            savepath = getpref('ECoG','SubjectInformation');
            filename = [savepath obj.Name '.info'];
            save(filename, 'obj');
            disp(['Subject information for ' obj.Name ' saved.']);
        end
        
        function dispOriginalData(obj)
            disp('--> Original data...');
            OriginalDataInfo.dispHeader()
            for i = 1:length(obj.OriginalData)
                OriginalDataInfo.dispRow(obj.OriginalData(i))
            end
            OriginalDataInfo.dispFooter()
            disp(' ');
        end
        
        function dispDatasets(obj)
            disp('--> Datasets...');
            DatasetInfo.dispTable(obj.Datasets)
            disp(' ');
        end
        
        function dispTaskData(obj)
            disp('--> Tasks...');
            TaskInfo.dispHeader()
            for i = 1:length(obj.Tasks)
                TaskInfo.dispRow(obj.Tasks(i))
            end
            TaskInfo.dispFooter()
            disp(' ');
        end
        
        function disp(obj)
            disp(['Subject Information for "' obj.Name '"']);
            disp(' ');
            disp(['        Description :  ' obj.Description]);
            disp(['     Primary prefix :  ' obj.PrimaryPrefix]);
            disp(['      Base filepath :  ' obj.FilepathBase]);
            disp(' ');
            obj.dispOriginalData;
            obj.dispDatasets;
            obj.dispTaskData;
        end
        
    end
    
    methods(Static)
        
        function obj = Load(subjectName)
            if nargin==1
                %LOAD Load the subject information for given name.
                savepath = getpref('ECoG','SubjectInformation');
                filename = [savepath subjectName '.info'];
                lTemp = load('-MAT', filename);
                obj = lTemp.obj;
                clear lTemp;
                disp(['Subject information for ' obj.Name ' loaded.']);
            else
                obj = [];
                list = SubjectInfo.List();
                for i = 1:length(list)
                    s = SubjectInfo.Load(list{i});
                    obj.(s.PrimaryPrefix) = s;
                end
            end
        end
        
        function list = List()
            %LIST Returns a list of all current subject information files.
            savepath = getpref('ECoG','SubjectInformation');
            l = dir([savepath '*.info']);
            
            for i=1:length(l)
                a = l(i).name;
                list(i) = {a(1:length(a)-5)};
            end
        end
        
        function b = Exist(subjectName)
            savepath = getpref('ECoG','SubjectInformation');
            if exist([savepath subjectName '.info'], 'file')
                b = true;
            else
                b = false;
            end
        end
        
        
        function Delete(subjectName)
            savepath = getpref('ECoG','SubjectInformation');
            delete([savepath subjectName '.info'])
        end
        
    end
    
end

